Skip to content

Conversation

@Saphereye
Copy link

@Saphereye Saphereye commented Nov 29, 2025

Fixes #149448

This PR fixes a potential integer overflow while calculating the children of a node for a BinaryHeap for zero sized types.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 29, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

Comment on lines 809 to 822
// If hole has only one child.
//
// child <= end - 1
// => 2 * hole + 1 <= end - 1
// => hole <= (end - 2) / 2
// => hole < 1 + (end - 2) / 2
// => hole < end / 2
if hole.pos() < end / 2 {
let child = 2 * hole.pos() + 1;
// If we are not in order, move parent
if hole.element() < unsafe { hole.get(child) } {
unsafe { hole.move_to(child) };
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't just delete the safety comments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although they also don't need to be complex; just need to point out why hole.pos() != child and both are valid indices.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry. I feel the new code is different than what we had before so the short circuit safety comment (before the if statement) can be removed? I will add the c = 2 * h + 1 safety comment back

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the point the original was making was just that cond && unsafe { can_assume_here_that(cond == true) }, which was kind of tangential to the actual unsafe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BinaryHeap integer overflow with ZST?

5 participants